home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE16 / ANIMPAL.C next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  272 lines

  1.  
  2. #include <windows.h>  
  3. #include "AnimPal.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. #define NUMBERCOLORS 128
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static LPLOGPALETTE pLogPal;
  112. static HPALETTE     hPal;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  int i;
  119.  
  120.                  // Allocate space for the logical palette.
  121.                  //........................................
  122.                  pLogPal = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
  123.                                       sizeof(LOGPALETTE) + 
  124.                                       (NUMBERCOLORS * sizeof(PALETTEENTRY)) );
  125.  
  126.                  // Initialize the palette with 1 red color and the
  127.                  // rest of the colors being white.
  128.                  //................................................
  129.                  pLogPal->palVersion    = 0x300;
  130.                  pLogPal->palNumEntries = NUMBERCOLORS;
  131.  
  132.                  for( i = 0; i < NUMBERCOLORS; i++ )
  133.                  {
  134.                     pLogPal->palPalEntry[i].peRed   = 255;
  135.                     pLogPal->palPalEntry[i].peGreen = (i==0 ? 0 : 255 );
  136.                     pLogPal->palPalEntry[i].peBlue  = (i==0 ? 0 : 255 );
  137.                     pLogPal->palPalEntry[i].peFlags = PC_RESERVED;
  138.                  }
  139.  
  140.                  // Create the palette.
  141.                  //....................
  142.                  hPal = CreatePalette( pLogPal );
  143.               }
  144.               break;
  145.  
  146.       case WM_PAINT :
  147.               {
  148.                  PAINTSTRUCT ps;
  149.                  HBRUSH      hBrush;
  150.                  int         i;
  151.  
  152.                  // Begin painting and select and realize the palette.
  153.                  //...................................................
  154.                  BeginPaint( hWnd, &ps );
  155.                  SelectPalette( ps.hdc, hPal, FALSE );
  156.                  RealizePalette( ps.hdc );
  157.  
  158.                  // Select a null pen.
  159.                  //...................
  160.                  SelectObject( ps.hdc, GetStockObject( NULL_PEN ) );
  161.  
  162.                  // Paint a rectangle in each of the
  163.                  // colors in our logical palette.
  164.                  //.................................
  165.                  for ( i = 0; i < NUMBERCOLORS; i++ )
  166.                  {
  167.                     // Create a brush of the palette color and select it.
  168.                     //...................................................
  169.                     hBrush = CreateSolidBrush( PALETTEINDEX(i) );
  170.                     SelectObject( ps.hdc, hBrush );
  171.  
  172.                     // Paint the rectangle.
  173.                     //.....................
  174.                     Rectangle( ps.hdc, i*4, 0, (i*4)+25, 50 );
  175.  
  176.                     // Select the stock white brush object.
  177.                     //.....................................
  178.                     SelectObject( ps.hdc, GetStockObject( WHITE_BRUSH ) );
  179.  
  180.                     // Delete the brush we created.
  181.                     //.............................
  182.                     DeleteObject( hBrush );
  183.                  }
  184.  
  185.                  // End painting.
  186.                  EndPaint( hWnd, &ps );
  187.               }
  188.               break;
  189.  
  190.       case WM_COMMAND :
  191.               switch( LOWORD( wParam ) )
  192.               {
  193.                  case IDM_TEST :
  194.                         {
  195.                            int i;
  196.                             
  197.                            // Cycle through each of the palette entries
  198.                            // changing on entry at a time to red and then
  199.                            // calling animate palette to show the change.
  200.                            //............................................
  201.                            for ( i = 0; i < NUMBERCOLORS; i++ )
  202.                            {
  203.                               pLogPal->palPalEntry[i].peGreen = 255;
  204.                               pLogPal->palPalEntry[i].peBlue  = 255;
  205.                               if ( i < NUMBERCOLORS-1 )
  206.                               {
  207.                                  pLogPal->palPalEntry[i+1].peGreen = 0;
  208.                                  pLogPal->palPalEntry[i+1].peBlue  = 0;
  209.                               }
  210.                               else
  211.                               {
  212.                                  pLogPal->palPalEntry[0].peGreen = 0;
  213.                                  pLogPal->palPalEntry[0].peBlue  = 0;
  214.                               }
  215.  
  216.                               AnimatePalette( hPal, 0, NUMBERCOLORS, 
  217.                                               pLogPal->palPalEntry );
  218.                            }
  219.                         }
  220.                         break;
  221.  
  222.                  case IDM_ABOUT :
  223.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  224.                         break;
  225.  
  226.                  case IDM_EXIT :
  227.                         DestroyWindow( hWnd );
  228.                         break;
  229.               }
  230.               break;
  231.       
  232.       case WM_DESTROY :
  233.               // Free the memory for the logical palette
  234.               // and delete the palette object handle.
  235.               //........................................
  236.               HeapFree( GetProcessHeap(), 0, pLogPal );
  237.               DeleteObject( hPal );
  238.  
  239.               PostQuitMessage(0);
  240.               break;
  241.  
  242.       default :
  243.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  244.    }
  245.  
  246.    return( 0L );
  247. }
  248.  
  249.  
  250. LRESULT CALLBACK About( HWND hDlg,           
  251.                         UINT message,        
  252.                         WPARAM wParam,       
  253.                         LPARAM lParam)
  254. {
  255.    switch (message) 
  256.    {
  257.        case WM_INITDIALOG: 
  258.                return (TRUE);
  259.  
  260.        case WM_COMMAND:                              
  261.                if (   LOWORD(wParam) == IDOK         
  262.                    || LOWORD(wParam) == IDCANCEL)    
  263.                {
  264.                        EndDialog(hDlg, TRUE);        
  265.                        return (TRUE);
  266.                }
  267.                break;
  268.    }
  269.  
  270.    return (FALSE); 
  271. }
  272.